from LyricEmotion.pyweka.arff import *

class RunWekaError(Exception): pass
class TrainFileNotExistError(RunWekaError):pass
class TestFileNotExistError(RunWekaError):pass
class EssentialParamNotExistError(RunWekaError):pass

def SMODetail(params):
    """
    @param the trainfile and testfile -t -T >
    # user define 
    -t classtrain.arff
    -T classtest.arff
    > smodisclassresult.txt

    # get from the arff file
    -p should get here

    # default
    -i
    -M
    -distribution
    """
    cmd = "java weka.classifiers.functions.SMO -C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K \"weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0\""
    defaultparams = {"-i":"","-M":"","-distribution":""}

    if (not params.has_key("-t")) or (not params.has_key("-T")) or (not params.has_key(">")):
        raise EssentialParamNotExistError("params not exist error")

    attrs = getAttrList(params["-t"])
    params["-p"] = str(len(attrs))

    for key in defaultparams.keys():
        cmd += " "+key+" "+defaultparams[key]
    
    for key in params.keys():
        if key == ">":
            continue
        cmd += " "+key+" "+params[key]
    cmd += " > "+params[">"]
    
    os.system(cmd)

def SMONoDetail(params):
    """
    @param the trainfile and testfile -t -T >
    # user define 
    -t classtrain.arff
    -T classtest.arff
    > smodisclassresult.txt

    # default
    -i
    """
    cmd = "java weka.classifiers.functions.SMO -C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K \"weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0\""
    defaultparams = {"-i":"",}

    if (not params.has_key("-t")) or (not params.has_key("-T")) or (not params.has_key(">")):
        raise EssentialParamNotExistError("params not exist error")

    for key in defaultparams.keys():
        cmd += " "+key+" "+defaultparams[key]
    
    for key in params.keys():
        if key == ">":
            continue
        cmd += " "+key+" "+params[key]
    cmd += " > "+params[">"]
    
    os.system(cmd)
    
def SMOTrain(params):
    """
    java weka.classifiers.trees.J48 -t /some/where/train.arff -d /other/place/j48.model
    -t classtrain.arff
    -d modelfile
    """
    cmd = "java weka.classifiers.functions.SMO -C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K \"weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0\""
    if (not params.has_key("-t") or (not params.has_key("-d"))):
        raise EssentialParamNotExistError("-t and -d is needed")
    for key in params.keys():
        cmd += " "+key+" "+params[key]
    os.system(cmd)


def SMODetailFromModel(params):
    """
    java weka.classifiers.trees.J48 -l /other/place/j48.model -T /some/where/test.arff
    -T 
    -l modelfile
    > smodisclassresult.txt

    # get from the arff file
    -p should get here

    # default
    -i
    -M
    -distribution
    """
    defaultparams = {"-i":"","-distribution":""}
    cmd = "java weka.classifiers.functions.SMO "
    if (not params.has_key("-T") or (not params.has_key("-l"))or (not params.has_key(">"))):
        raise EssentialParamNotExistError("-t and -d is needed")
    
    for key in defaultparams.keys():
        cmd += " "+key+" "+defaultparams[key]

    attrs = getAttrList(params["-T"])
    params["-p"] = str(len(attrs))

    for key in params.keys():
        if key == ">":
            continue
        cmd += " "+key+" "+params[key]
    cmd += " > "+params[">"]
    #print cmd
    os.system(cmd)

